package com.lc1993929.mr.join;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * Created by LiuChang on 2017/12/14/014.
 */
public class JoinMapper extends Mapper<LongWritable, Text, Text, JoinBean> {

    String fileName;

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        FileSplit inputSplit = (FileSplit) context.getInputSplit();
        fileName = inputSplit.getPath().getName();
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] split = line.split(",");
        JoinBean joinBean = new JoinBean();
        if ("order.txt".equals(fileName)) {
            joinBean.setOrderid(split[0]);
            joinBean.setUid(split[1]);
            joinBean.setTableName(fileName);
            joinBean.setUname("null");
            joinBean.setAge("null");
            joinBean.setLover("null");
        } else {
            joinBean.setUid(split[0]);
            joinBean.setUname(split[1]);
            joinBean.setAge(split[2]);
            joinBean.setLover(split[3]);
            joinBean.setTableName(fileName);
            joinBean.setOrderid("null");
        }
        context.write(new Text(joinBean.getUid()), joinBean);
    }
}
